var CustomerForm = function () {
  var template = /*html*/ `
  <div class="customerForm" v-loading="loading">
    <el-form class="customer-form" :model="customer" label-width="120px">
      <el-form-item :label="customerType===1?'供应商代码':'客户代码'">
        <el-input class="form-input" v-if="type!=='update'" v-model="customer.customerCode"></el-input>
        <span v-else>{{customer.customerCode}}</span>
      </el-form-item>
      <el-form-item :label="customerType===1?'供应商名称':'客户名称'">
        <el-input class="form-input" v-if="type!=='update'" v-model="customer.customerName"></el-input>
        <span v-else>{{customer.customerName}}</span> 
      </el-form-item> 
      <el-form-item label="选择类型">
        <div v-if="customerList.length===1">
          {{customerList[0].label}}
        </div>
        <el-select v-else v-model="customer.type">
          <el-option v-for="customer in customerList" :key="'customer_'+customer.value" :value="customer.value" :label="customer.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input class="form-input" v-model="customer.contact"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input class="form-input" v-model="customer.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input class="form-input" v-model="customer.email" type="textarea" :autosize="autosizeOption" resize="none" placeholder="多个邮箱换行或者;隔开"></el-input>
      </el-form-item>
      <el-form-item label="登录密码" v-if="customer.type ===2 || customer.type ===3 || customerType ===2">
        <el-input class="form-input" v-model="customer.password"></el-input>
      </el-form-item>
      <el-form-item label="付款结算方式">
        <el-select class="form-input" v-model="customer.paymentWay">
          <el-option v-for="item in sumMoneyType" 
            :key="item.value" 
            :value="item.value" 
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select class="form-input" v-model="customer.status">
          <el-option label="使用" value="使用"></el-option>
          <el-option label="禁用" value="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否含税">
        <el-select class="form-input" v-model="customer.isTax">
          <el-option label="含税" :value="0"></el-option>
          <el-option label="不含税" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运费负担">
        <el-select class="form-input" v-model="customer.preight">
          <el-option label="我司" :value="0"></el-option>
          <el-option v-if="customerType===1" label="供方" :value="1"></el-option>
          <el-option v-else label="客户" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="传真">
        <el-input class="form-input"  v-model="customer.fax"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input class="form-input"  v-model="customer.address"></el-input>
      </el-form-item>
      <template v-if="type==='update'">
        <el-form-item label="创建人">
          <span>{{customer.creater}}</span>
        </el-form-item>
      </template>
      <template v-if="type==='update'">
        <el-form-item label="创建时间">
          <span>{{customer.creationTime}}</span>
        </el-form-item>
      </template>
      <template v-if="type==='update'">
        <el-form-item label="最后修改人">
          <span>{{customer.lastModifier}}</span>
        </el-form-item> 
      </template>
      <template v-if="type==='update'">
        <el-form-item label="最后修改时间">
          <span>{{customer.lastModifyTime}}</span>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary" v-if="type==='add'" @click="addCustomer">保存</el-button>
        <el-button type="primary" v-else-if="customer.id !==''" @click="updateCustomer">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
  `;
  return {
    props: {
      //操作类型 add 添加 update 修改
      type: {
        type: String,
        default: 'add'
      },
      //类型 1供应商 2(客户 3供应商+客户)
      customerType: {
        type: Number,
        required: true
      },
      customer: {
        type: Object,
        default: function () {
          return new Customer();
        }
      }
    },
    data: function () {
      var customerList;
      if (this.type === 'add') {
        if (this.customerType === 1) {
          customerList = [1];
        } else {
          customerList = [2, 3]
        }
      } else {
        customerList = [1, 2, 3];
      }
      customerList = i18n.getDropDown('customer.customerType', customerList);
      return {
        loading: false,
        sumMoneyType: i18n.getDropDown('customer.paymentWayType', ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '18',
          '24'
        ]),
        customerList: customerList,
        emailReg: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
        autosizeOption: {
          minRows: 2,
          maxRows: 8
        }
      }
    },
    methods: {
      //保存新增
      addCustomer: function () {
        var customerData = this.customer;
        if (this.customerType === 1) {
          customerData.type = 1;
        }
        if (!customerData.checkValid()) {
          return;
        }
        var postData = customerData.getObject();
        var emailVaild = customerData.checkEmailValid(postData.email, this.emailReg);
        if (!emailVaild) {
          this.$alert("请输入有效email", "信息");
          return;
        }
        this.loading = true;
        var self = this;
        ajaxRequest("basicData/insertCustomer", postData).then(function (data) {
          self.$alert("添加成功", "信息")
          self.$emit('save-success', postData);
          self.loading = false;
        }).catch(function () {
          self.$emit('save-error');
          self.loading = false;
        });
      },
      //修改
      updateCustomer: function () {
        var customerData = this.customer;
        if (!customerData.checkValid()) {
          return;
        }
        var postData = customerData.getObject();
        var emailVaild = customerData.checkEmailValid(postData.email, this.emailReg);
        if (!emailVaild) {
          this.$alert("请输入有效email", "信息");
          return;
        }
        this.loading = true;
        var self = this;
        ajaxRequest('basicData/updateCustomer', postData).then(function (data) {
          self.$alert("修改成功", "信息")
          self.$emit('save-success');
          self.loading = false;
        }).catch(function () {
          self.$emit('save-error');
          self.loading = false;
        })
      },
    },
    template: template
  }
}();
